﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NTQOnlineExam.Services.DBCommand;
using NTQOnlineExam.Services.Infrastructure;
using NTQOnlineExam.Services.Infrastructure.Utility;
using NTQOnlineExam.Services.ReportService.Question;

namespace NTQOnlineExam.Services.CommandService.Question
{
    public class QuestionCommandService : IQuestionCommandService
    {
        private readonly ICommandExecutor _executor;
        private readonly IQuestionReportService _questionReportService;
        public QuestionCommandService(ICommandExecutor executor, IQuestionReportService questionReportService)
        {
            _executor = executor;
            _questionReportService = questionReportService;
        }

        public bool CreateQuestion(AddQuestionCommand addQuestionCommand, out int questionId)
        {
            Guard.ArgumentIsNotNull(addQuestionCommand, "command");
            Guard.Against<ArgumentException>(addQuestionCommand.TermId <= 0, "Term Id is required");
            Guard.Against<ArgumentException>(addQuestionCommand.SectionId <= 0, "Section Id is required");
            Guard.Against<ArgumentException>(String.IsNullOrEmpty(addQuestionCommand.Content), "Content can not be null or empty!");
            var result = DBCommand.DBCommand.CreateInstant().CreateQuestionDbCommand(addQuestionCommand, out questionId);
            return result;
        }

        public bool UpdateQuestion(UpdateQuestionCommand questionCommand)
        {
            Guard.ArgumentIsNotNull(questionCommand, "command");
            Guard.Against<ArgumentException>(questionCommand.TermId <= 0, "Term Id is required");
            Guard.Against<ArgumentException>(questionCommand.SectionId <= 0, "Section Id is required");
            Guard.Against<ArgumentException>(String.IsNullOrEmpty(questionCommand.Content), "Content can not be null or empty!");
            var result = DBCommand.DBCommand.CreateInstant().UpdateQuestionDbCommand(questionCommand);
            return result;
        }

        public bool DeleteQuestion(int questionId)
        {
            Guard.Against<ArgumentException>(questionId <= 0, "QuestionId is required");
            var result = DBCommand.DBCommand.CreateInstant().DeleteQuestionDbCommand(questionId);
            return result;
        }

        public bool ChangeQuestionOrder(ChangeQuestionOrderCommand command)
        {
            return _executor.Execute(new ChangeQuestionOrderDbCommand(command.QuestionId, command.NewOrder));
        }

        private void ReArrangeSectionOrder(int sectionId, int termId)
        {
            _executor.Execute(new ReArrangeQuestionOrderDbCommand(termId, sectionId));
        }
    }
}
